package com.andova.image

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.andova.libyuv.TestNEON
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class NEONTest {
    companion object {
        const val BENCHMARK_WIDTH = 4
        const val BENCHMARK_HEIGHT = 3
    }

    @Suppress("LocalVariableName")
    @Test
    fun testNEON_VTRN() {
        for (i in 0..Int.MAX_VALUE) {
            val src_u = byteArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24)
            val src_v = byteArrayOf(31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54)
            val dst_uv = ByteArray((src_u.size + src_v.size))
            TestNEON.TestNEON_VTRN(src_u, src_v, dst_uv, dst_uv.size)
            println(dst_uv)
        }
    }

    @Suppress("LocalVariableName")
    @Test
    fun testHalfMerge() {
        val src_u = byteArrayOf(1, 9, 3, 7, 5, 2, 4, 8, 6, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)
        val src_v = byteArrayOf(41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72)
        val dst_uv = ByteArray((src_u.size + src_v.size) / 2)
        TestNEON.TestHalfMerge(src_u, src_v, dst_uv, dst_uv.size)
        println(src_u)
        println(dst_uv)
    }

    @Suppress("LocalVariableName")
    @Test
    fun testNEON_VPADDL() {
        val src_u = byteArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)
        val src_v = byteArrayOf(41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72)
        val dst_u = ByteArray(src_u.size)
        val dst_v = ByteArray(src_v.size)
        TestNEON.TestNEON_VPADDL(src_u, src_v, dst_u, dst_v, dst_u.size + dst_v.size)
        println(src_u, "src_u")
        println(dst_u, "dst_u")
        println(src_v, "src_v")
        println(dst_v, "dst_v")
    }

    @Suppress("LocalVariableName")
    @Test
    fun testNEON_VPADAL() {
        val src_u = byteArrayOf(1, 2, 3, 4, 5, 6, 7, 8)
        val src_v = byteArrayOf(9, 10, 11, 12, 13, 14, 15, 16)
        val dst_u = ByteArray(src_u.size)
        val dst_v = ByteArray(src_v.size)
        TestNEON.TestNEON_VPADAL(src_u, src_v, dst_u, dst_v, dst_u.size + dst_v.size)
        println(src_u, "src_u")
        println(dst_u, "dst_u")
        println(src_v, "src_v")
        println(dst_v, "dst_v")
    }

    @Suppress("LocalVariableName")
    @Test
    fun testNEON_VQRSHRN() {
        val src_u = byteArrayOf(2, 9, 33, 34, 15, 53, 78, 20)
        val src_v = byteArrayOf(9, 0, 11, 0, 13, 0, 15, 0)
        val dst_u = ByteArray(src_u.size)
        val dst_v = ByteArray(src_v.size)
        TestNEON.TestNEON_VQRSHRN(src_u, src_v, dst_u, dst_v, dst_u.size + dst_v.size)
        println(src_u, "src_u")
        println(dst_u, "dst_u")
        println(src_v, "src_v")
        println(dst_v, "dst_v")
    }

    private fun println(data: ByteArray, head: String? = null) {
        val sb = StringBuilder()
        sb.append("${if (head.isNullOrEmpty()) "" else "$head:"}[")
        for (i in data) {
            sb.append(i)
            sb.append(',')
        }
        sb.delete(sb.length - 1, sb.length)
        sb.append(']')
        println(sb)
    }
}